<?php

if (!defined('IN_IA')) {
    die( 'Access Denied' );
}

class courier{

    public $tableExpress = 'ewei_shop_plugin_express';
    public $tableDispatch = 'ewei_shop_dispatch';
    public $tableCourier = 'ewei_shop_plugin_express_courier';
    public $tableCourierLists = 'ewei_shop_plugin_express_courier_lists';
    
    /**
     * 获取快递预处理的物流单号列表
     * @param $expressId
     * @return array
     */
    public function getCourierLists($expressId){
        global $_W;

        if(empty($expressId)){
            return false;
        }

        $sql  = ' SELECT c.*, d.dispatchname FROM '.tablename($this->tableCourier).' AS c';
        $sql .= ' LEFT JOIN '.tablename($this->tableExpress).' AS e ON c.`expressid` = e.`id` ';
        $sql .= ' LEFT JOIN '.tablename($this->tableDispatch).' AS d ON e.`dispatchid` = d.`id` ';
        $sql .= ' WHERE c.`uniacid` = :uniacid AND c.`expressid` = :expressId ';

        $result = pdo_fetchall($sql, array(':uniacid' => $_W['uniacid'], ':expressId' => $expressId), 'id');

        if(!empty($result)){

            $sql = 'SELECT count(id) FROM '.tablename($this->tableCourierLists).' WHERE `courierid` = :courierid AND `status` = :status';

            $pk = 1;
            foreach($result as $key => $value){
                $result[$key]['pk'] = $pk;
                $result[$key]['use'] = pdo_fetchcolumn($sql, array(':courierid' => $value['id'], ':status' => 1));
                $result[$key]['oth'] = pdo_fetchcolumn($sql, array(':courierid' => $value['id'], ':status' => 0));
                $pk ++;
            }
        }

        return $result;
    }

    /**
     * 获取快递预处理的物流单号列表
     * @param $expressId
     * @return array
     */
    public function getCourierListsTotal($expressId, $status = 0){
        global $_W;

        if(empty($expressId)){
            return false;
        }

        $total = 0;
        $lists = $this->getCourierLists($expressId);

        if(!empty($lists)){
            foreach($lists as $key => $list){
                $sql = 'SELECT count(id) FROM '.tablename($this->tableCourierLists).' WHERE `courierid` = :courierid AND `status` = :status ';
                $lists[$key]['total'] = pdo_fetchcolumn($sql, array(':courierid' => $list['id'], ':status' => $status));

                $total += $lists[$key]['total'];
            }
        }

        return array('lists' => $lists, 'total' => $total);
    }

    /**
     * 获取单个单号池数据
     * @param $courierId
     * @param $pIndex
     * @param array $status
     * @return array
     */
    public function getCourierDetail($courierId, $pIndex, $size = 20, $whereSql = ''){
        $params =  array(':courierid' => $courierId);

        $totalSql = 'SELECT COUNT(id) FROM '.tablename($this->tableCourierLists).' WHERE `courierid` = :courierid ';
        $total = pdo_fetchcolumn($totalSql . $whereSql, $params);
        $result['pagination'] = pagination($total, $pIndex, $size);

        $detailSql = 'SELECT * FROM '.tablename($this->tableCourierLists).' WHERE `courierid` = :courierid ';
        $whereSql .= ' ORDER BY `id` ASC LIMIT '. ($pIndex - 1) * $size .','. $size;
        $result['detail']  = pdo_fetchall($detailSql . $whereSql, $params, 'id');

        return $result;
    }

    /**
     * 获取指定数量的物流单号
     * @param $courierId
     * @param int $total
     * @return array
     */
    public function getCourierListsByTotal($courierId, $total = 50){
        $where = '';
        $params = array();

        if(!is_array($courierId)){
            $params =  array(':courierid' => $courierId);
            $where = ' WHERE `courierid` = :courierid AND `status` = 0';
        }else{
            $where = ' WHERE `courierid` IN ('.implode(',', $courierId).') AND `status` = 0';
        }

        $sql  = 'SELECT `id`, `number` FROM '.tablename($this->tableCourierLists);
        $sql .= $where;
        $sql .= ' ORDER BY `id` ASC LIMIT '.$total;

        return pdo_fetchall($sql, $params);
    }

    /**
     * 获取指定订单ID的物流单号
     * @param $orderid
     * @return array
     */
    public function getListsByOrderId($orderid, $goodsid){
        global $_W;

        $sql = 'SELECT * FROM '.tablename($this->tableCourierLists).' WHERE `uniacid` = :uniacid AND `orderid` = :orderid AND `goodsid` = :goodsid';
        $param = array(':uniacid' => $_W['uniacid'], ':orderid' => $orderid, ':goodsid' => $goodsid);

        return pdo_fetchall($sql, $param);
    }

}